{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c2b9b673",
   "metadata": {},
   "source": [
    "## 金融函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "e6aaea1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3109.52"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 等额本息，计算每月还款\n",
    "debx(amount=500000,rate=0.043,period=240)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "230a3497",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>期数</th>\n",
       "      <th>每月还款</th>\n",
       "      <th>本期本金</th>\n",
       "      <th>本期利息</th>\n",
       "      <th>本期剩余</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>3109.52</td>\n",
       "      <td>1317.85</td>\n",
       "      <td>1791.67</td>\n",
       "      <td>498682.15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>3109.52</td>\n",
       "      <td>1322.58</td>\n",
       "      <td>1786.94</td>\n",
       "      <td>497359.57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>3109.52</td>\n",
       "      <td>1327.31</td>\n",
       "      <td>1782.21</td>\n",
       "      <td>496032.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>3109.52</td>\n",
       "      <td>1332.07</td>\n",
       "      <td>1777.45</td>\n",
       "      <td>494700.19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>3109.52</td>\n",
       "      <td>1336.84</td>\n",
       "      <td>1772.68</td>\n",
       "      <td>493363.35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>235</th>\n",
       "      <td>236</td>\n",
       "      <td>3109.52</td>\n",
       "      <td>3054.40</td>\n",
       "      <td>55.12</td>\n",
       "      <td>12328.63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>236</th>\n",
       "      <td>237</td>\n",
       "      <td>3109.52</td>\n",
       "      <td>3065.34</td>\n",
       "      <td>44.18</td>\n",
       "      <td>9263.29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>237</th>\n",
       "      <td>238</td>\n",
       "      <td>3109.52</td>\n",
       "      <td>3076.33</td>\n",
       "      <td>33.19</td>\n",
       "      <td>6186.96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>238</th>\n",
       "      <td>239</td>\n",
       "      <td>3109.52</td>\n",
       "      <td>3087.35</td>\n",
       "      <td>22.17</td>\n",
       "      <td>3099.61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>239</th>\n",
       "      <td>240</td>\n",
       "      <td>3109.52</td>\n",
       "      <td>3098.41</td>\n",
       "      <td>11.11</td>\n",
       "      <td>1.20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>240 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      期数    每月还款    本期本金    本期利息      本期剩余\n",
       "0      1 3109.52 1317.85 1791.67 498682.15\n",
       "1      2 3109.52 1322.58 1786.94 497359.57\n",
       "2      3 3109.52 1327.31 1782.21 496032.26\n",
       "3      4 3109.52 1332.07 1777.45 494700.19\n",
       "4      5 3109.52 1336.84 1772.68 493363.35\n",
       "..   ...     ...     ...     ...       ...\n",
       "235  236 3109.52 3054.40   55.12  12328.63\n",
       "236  237 3109.52 3065.34   44.18   9263.29\n",
       "237  238 3109.52 3076.33   33.19   6186.96\n",
       "238  239 3109.52 3087.35   22.17   3099.61\n",
       "239  240 3109.52 3098.41   11.11      1.20\n",
       "\n",
       "[240 rows x 5 columns]"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 等额本息，查看还款计划\n",
    "a = debx(amount=500000,rate=0.043,period=240,return_data=True)\n",
    "a['data']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "dd034198",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>期数</th>\n",
       "      <th>每月还款</th>\n",
       "      <th>本期本金</th>\n",
       "      <th>本期利息</th>\n",
       "      <th>本期剩余</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>3875.00</td>\n",
       "      <td>2083.33</td>\n",
       "      <td>1791.67</td>\n",
       "      <td>497916.67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>3867.53</td>\n",
       "      <td>2083.33</td>\n",
       "      <td>1784.20</td>\n",
       "      <td>495833.34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>3860.07</td>\n",
       "      <td>2083.33</td>\n",
       "      <td>1776.74</td>\n",
       "      <td>493750.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>3852.60</td>\n",
       "      <td>2083.33</td>\n",
       "      <td>1769.27</td>\n",
       "      <td>491666.68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>3845.14</td>\n",
       "      <td>2083.33</td>\n",
       "      <td>1761.81</td>\n",
       "      <td>489583.35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>235</th>\n",
       "      <td>236</td>\n",
       "      <td>2120.66</td>\n",
       "      <td>2083.33</td>\n",
       "      <td>37.33</td>\n",
       "      <td>8334.12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>236</th>\n",
       "      <td>237</td>\n",
       "      <td>2113.20</td>\n",
       "      <td>2083.33</td>\n",
       "      <td>29.87</td>\n",
       "      <td>6250.79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>237</th>\n",
       "      <td>238</td>\n",
       "      <td>2105.73</td>\n",
       "      <td>2083.33</td>\n",
       "      <td>22.40</td>\n",
       "      <td>4167.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>238</th>\n",
       "      <td>239</td>\n",
       "      <td>2098.27</td>\n",
       "      <td>2083.33</td>\n",
       "      <td>14.94</td>\n",
       "      <td>2084.13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>239</th>\n",
       "      <td>240</td>\n",
       "      <td>2090.80</td>\n",
       "      <td>2083.33</td>\n",
       "      <td>7.47</td>\n",
       "      <td>0.80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>240 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      期数    每月还款    本期本金    本期利息      本期剩余\n",
       "0      1 3875.00 2083.33 1791.67 497916.67\n",
       "1      2 3867.53 2083.33 1784.20 495833.34\n",
       "2      3 3860.07 2083.33 1776.74 493750.01\n",
       "3      4 3852.60 2083.33 1769.27 491666.68\n",
       "4      5 3845.14 2083.33 1761.81 489583.35\n",
       "..   ...     ...     ...     ...       ...\n",
       "235  236 2120.66 2083.33   37.33   8334.12\n",
       "236  237 2113.20 2083.33   29.87   6250.79\n",
       "237  238 2105.73 2083.33   22.40   4167.46\n",
       "238  239 2098.27 2083.33   14.94   2084.13\n",
       "239  240 2090.80 2083.33    7.47      0.80\n",
       "\n",
       "[240 rows x 5 columns]"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 等额本金，查看还款计划\n",
    "a = debj(amount=500000,rate=0.043,period=240,return_data=True)\n",
    "a['data']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3635c3ca",
   "metadata": {},
   "source": [
    "## 数学计算函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "bb6c53b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8415"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sin(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "6cbe1329",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5403"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cos(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1efd3f6f",
   "metadata": {},
   "source": [
    "## 日期计算函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "31dfbe06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2023, 1, 1, 0, 0)"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "year_start('2023-05-23')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "43463108",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2023, 12, 31, 0, 0)"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "year_end('2023-05-23')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "8cbed869",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2023, 5, 1, 0, 0)"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "month_start('2023-05-23')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "8403db56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2023, 4, 30, 0, 0)"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "month_end('2023-04-23')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "a9bf7cf5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2023, 2, 28, 0, 0)"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add_months('2023-04-30',-2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "16559f1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算日期差\n",
    "date_diff('2023-05-01','2023-05-16')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "e9145cb2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算月份差\n",
    "date_diff('2023-05-01','2023-05-16','m')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "f8c72bb0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算年份差\n",
    "date_diff('2022-05-01','2023-05-02','y')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "611a5158",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算月份差\n",
    "month_diff('2023-05-01','2023-05-16')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "1fb2d00b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算月份差, 计算时日期按照1日进行计算，例如 '2023-05-15' 按 '2023-05-01'，'2023-06-16' 按'2023-06-01'进行计算 \n",
    "month_diff('2023-05-15','2023-06-16',method='mm')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca721e7b",
   "metadata": {},
   "source": [
    "## 正则函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "692addb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 正则查找函数\n",
    "regexp_like('2023','[a-z]+')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "275b0b4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'westat'"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 正则替换函数\n",
    "regexp_replace('2023westat2023','[0-9]+','')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c941ecc7",
   "metadata": {},
   "source": [
    "## 其他统计函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "2e791b05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取指定值最大连续次数\n",
    "get_max_continue([1,2,2,1,2,2,2,0],2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "335a9221",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取连续大于指定值的次数,例如：检查列表中连续大于1的次数\n",
    "get_over_continue([1,2,2,1,2,3,4,0],1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "ca3613f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取大于指定值2，且相等的最大连续次数\n",
    "get_over_continue([3,4,2,1,2,3,3,4,0],2,eq=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "9fff0a11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# nvl函数，如果变量取值为空，则赋值为另一个值\n",
    "a=None\n",
    "nvl(a,5)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
